package com.example.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.Constants;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.User;
import com.example.entity.dto.LoginDto;
import com.example.entity.dto.PasswordReqDto;
import com.example.entity.vo.UserVo;
import com.example.exception.CustomException;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import com.example.utils.SaUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 用户接口实现类
 * @Author dingchunlin
 * @Date 2024/06/22 20:38
 * @Version 1.0.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 新增
     *
     * @param user
     */
    @Override
    public void add(User user)  {
        User dbUser = userMapper.selectByUsername(user.getUsername());
        if (ObjectUtil.isNotNull(dbUser)) {
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        if (ObjectUtil.isEmpty(user.getPassword())) {
            user.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if (ObjectUtil.isEmpty(user.getName())) {
            user.setName(user.getUsername());
        }
        userMapper.insert(user);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void deleteBatch(List<Integer> ids) {
        userMapper.deleteBatchIds(ids);
    }

    /**
     * 修改
     *
     * @param user
     */
    @Override
    public void updateUser(User user) {
        userMapper.updateById(user);
    }

    /**
     * 根据ID查询
     *
     * @param id
     */
    @Override
    public User selectById(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }

    /**
     * 查询所有
     *
     * @param user
     */
    @Override
    public List<User> selectAll(User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(user.getId()), User::getId, user.getId());
        lambdaQueryWrapper.like(ObjectUtil.isNotEmpty(user.getUsername()), User::getUsername, user.getUsername());
        return userMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 分页查询
     *
     * @param user
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageInfo<User> selectPage(User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = selectAll(user);
        String loginId = StpUtil.getLoginId().toString();
        System.out.println(loginId);
        return PageInfo.of(list);
    }

    /**
     * 登录
     *
     * @param loginDto
     */
    @Override
    public UserVo login(LoginDto loginDto)  {
        User dbUser = userMapper.selectByUsername(loginDto.getUsername());
        if (ObjectUtil.isNull(dbUser)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!loginDto.getPassword().equals(dbUser.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(dbUser, userVo);
        // 返回token
        StpUtil.login(dbUser.getId());
        String token = StpUtil.getTokenValue();
        userVo.setToken(token);
        userVo.setPassword(null);
        //进行返回
        return userVo;
    }

    /**
     * 注册
     *
     * @param user
     */
    @Override
    public void register(User user) {
        user.setRole(RoleEnum.USER.name());
        add(user);
    }

    /**
     * 修改密码
     *
     * @param passwordReqDto
     */
    @Override
    public void updatePassword(PasswordReqDto passwordReqDto) {
        User dbUser = userMapper.selectById(passwordReqDto.getId());
        if (ObjectUtil.isNull(dbUser)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        User loginUser = SaUtils.getLoginUser();
        //如果不是管理员，则修改进行校验密码是否正确
        if (!loginUser.getRole().equals(RoleEnum.ADMIN.name())){
            if (!passwordReqDto.getPassword().equals(dbUser.getPassword())){
                throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
            }
        }
        //设置新密码
        dbUser.setPassword(passwordReqDto.getNewPassword());
        userMapper.updateById(dbUser);
    }

}
